package com.jjb.www.ui.activity;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.view.View;

import com.hjq.bar.TitleBar;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeRecyclerView;
import com.hjq.shape.view.ShapeTextView;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.http.api.CheckUserPaperApi;
import com.jjb.www.http.api.EditUserSentryApi;
import com.jjb.www.http.api.ShareApi;
import com.jjb.www.http.api.UserPaperV2ListApi;
import com.jjb.www.http.api.UserPaperV2NoticeApi;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.ui.adapter.CpAdapter;
import com.jjb.www.ui.adapter.MsgAdapter;
import com.jjb.www.ui.dialog.CommonUseDialog;
import com.jjb.www.ui.dialog.NoUserVipDialog;

/**
 * author : Android CRG
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 * desc   : 模拟考试
 */
public class DataActivity extends AppActivity implements BaseAdapter.OnItemClickListener {
    private TitleBar mTitleBar;
    private ShapeTextView mTvLeft;
    private ShapeTextView mTvRight;
    private ShapeRecyclerView mRvMsg;
    private MsgAdapter msgAdapter;
    private ShapeRecyclerView mRvCp;
    private CpAdapter cpAdapter;

    //点击的true是10分钟还是false60分钟
    private boolean leftOrRight = true;
    private Integer type1 = 0;
    private Integer type2 = 0;
    //传进来的岗位名称
    private String name = "";
    //岗位id
    private Integer sentry_id = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_data;
    }

    @Override
    protected void initView() {
        mTitleBar = findViewById(R.id.titleBar);
        mTvLeft = findViewById(R.id.tv_left);
        mTvRight = findViewById(R.id.tv_right);
        mRvMsg = findViewById(R.id.rv_msg);
        mRvCp = findViewById(R.id.rv_cp);
        mRvCp.setLayoutManager(new LinearLayoutManager(getContext()));
        cpAdapter = new CpAdapter(getContext());
        cpAdapter.setOnItemClickListener(this);
        mRvCp.setAdapter(cpAdapter);
        msgAdapter = new MsgAdapter(getContext());
        mRvMsg.setLayoutManager(new LinearLayoutManager(getContext()));
        mRvMsg.setAdapter(msgAdapter);
        setOnClickListener(mTvLeft,mTvRight);
    }

    @Override
    protected void initData() {
        name = getString("name");
        sentry_id = getInt("sentry_id");
        mTitleBar.setTitle(name);
        //获取顶部按钮的值
        getButton();

    }

    private void getList(Integer type) {
        EasyHttp.get(this)//岗前蓝 继教红 其余黄
                .api(new UserPaperV2ListApi()
                        .setSentry_id(sentry_id+"")
                        .setType(type)
                )
                .request(new HttpCallback<HttpList<UserPaperV2ListApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpList<UserPaperV2ListApi.Bean> data) {
                        cpAdapter.setData(data.getData());
                    }
                });
    }

    private void getShare() {
        EasyHttp.get(this)//岗前蓝 继教红 其余黄
                .api(new ShareApi()
                )
                .request(new HttpCallback<HttpData<ShareApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<ShareApi.Bean> data) {

                    }
                });
    }

    private void getMag(Integer type) {
        EasyHttp.get(this)//岗前蓝 继教红 其余黄
                .api(new UserPaperV2NoticeApi()
                        .setSentry_id(sentry_id+"")
                        .setType(type)
                )
                .request(new HttpCallback<HttpData<UserPaperV2NoticeApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<UserPaperV2NoticeApi.Bean> data) {
                        msgAdapter.setData(data.getData().getNotice());
                    }
                });
    }

    private void getButton() {
        EasyHttp.put(this)//岗前蓝 继教红 其余黄
                .api(new EditUserSentryApi()
                .setSentry_id(sentry_id+""))
                .request(new HttpCallback<HttpData<EditUserSentryApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<EditUserSentryApi.Bean> data) {
                        if (data.getData().getType1()==null||data.getData().getType2()==null){
                            mTvRight.setVisibility(View.GONE);
                            return;
                        }else {
                            type1 = data.getData().getType1().getType();
                            type2 =data.getData().getType2().getType();
                            mTvLeft.setText(data.getData().getType1().getName());
                            mTvRight.setText(data.getData().getType2().getName());
                        }
                        //获取消息
                        getMag(type1);
                        getList(type1);
                        getShare();
                    }
                });
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.tv_left){
            leftOrRight = true;
            getMag(type1);
            getList(type1);
            getShare();
            mTvLeft.getShapeDrawableBuilder()
                    .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                    .intoBackground();
            mTvLeft.getTextColorBuilder()
                    .setTextColor(ContextCompat.getColor(getContext(), R.color.white))
                    .intoTextColor();

            mTvRight.getShapeDrawableBuilder()
                    .setSolidColor(ContextCompat.getColor(getContext(), R.color.white))
                    .setStrokeColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                    .setStrokeWidth(1)
                    .intoBackground();
            mTvRight.getTextColorBuilder()
                    .setTextColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                    .intoTextColor();

        }else if (view.getId()==R.id.tv_right){
            leftOrRight = false;
            getMag(type2);
            getList(type2);
            getShare();
            mTvRight.getShapeDrawableBuilder()
                    .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                    .intoBackground();
            mTvRight.getTextColorBuilder()
                    .setTextColor(ContextCompat.getColor(getContext(), R.color.white))
                    .intoTextColor();

            mTvLeft.getShapeDrawableBuilder()
                    .setSolidColor(ContextCompat.getColor(getContext(), R.color.white))
                    .setStrokeColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                    .setStrokeWidth(1)
                    .intoBackground();
            mTvLeft.getTextColorBuilder()
                    .setTextColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                    .intoTextColor();
        }
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        //刷试题限制
        if (leftOrRight){
            getCheckUserPaper(type1);
        }else {
            getCheckUserPaper(type2);
        }

    }
    private void getCheckUserPaper(Integer quickOrMock) {
        EasyHttp.post(this)
                .api(new CheckUserPaperApi()
                        .setExam_type(quickOrMock)
                )
                .request(new HttpCallback<HttpData<CheckUserPaperApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<CheckUserPaperApi.Bean> data) {
                        if (data.getData().getUser_day_count()<data.getData().getPaper_count()){
                            Intent intent = new Intent();
                            intent.putExtra("quick",quickOrMock);
                            intent.putExtra("sentry_id",sentry_id);
//                            intent.putExtra("student_id",student_id+"");
                            intent.setClass(getContext(), SequentialPracticeActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            new NoUserVipDialog.Builder(getContext())
                                    .setListener(new NoUserVipDialog.OnListener() {
                                        @Override
                                        public void onCompleted(BaseDialog dialog) {
                                            startActivity(VipActivity.class);
                                        }

                                        @Override
                                        public void onCancel(BaseDialog dialog) {

                                        }
                                    }).show();
                        }
                    }
                });
    }
}